home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / dnet / mailchk.lha / Mailchk / amiga / client / mailchk.c < prev   
Encoding:
C/C++ Source or Header  |  1990-03-05  |  14.0 KB  |  570 lines

  1.  
  2. /*
  3.  *    MAILCHK.C
  4.  *
  5.  *    DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved
  6.  *
  7.  *      Check the mailbox and reports if new mail
  8.  *      has arrived.
  9.  *      
  10.  *      Written by S. Laroche.  
  11.  *    
  12.  *      Usage:  mailchk -N(host) -q -d -t(time)
  13.  *              -d:  Display old mail on startup, if it exists
  14.  *              -q:  Quiet, don't display old mail on startup
  15.  *              Default:  -d
  16.  *
  17.  *              -t:  (time) is the number of seconds between checks.
  18.  *              Default:  40 seconds
  19.  *
  20.  *              -h:  help message
  21.  *
  22.  *      Arp.library is needed... (Version 39.1)
  23.  *      File requester doesn't work, or at least, not with this code.
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <local/typedefs.h>
  28. #include <arp/arpbase.h>
  29. #include "/dnet/channel.h"
  30. #include "/server/servers.h"
  31. #include <local/deemu.h>
  32.  
  33. short Deemu[] = {
  34.     DMSTRT, 0, 0,
  35.     DMNW  , 0, 10, 2, 2, -80, 40, 0xFFFF,
  36.     DMEND , 0, 0
  37. };
  38.  
  39. #define DMNWOFF 4
  40.  
  41. ITEXT IText[] = {
  42.     { 0, 1, JAM2, 0, 0, NULL, (ubyte *)"View"           },
  43.     { 0, 1, JAM2, 0, 0, NULL, (ubyte *)"Print"          },
  44.     { 0, 1, JAM2, 0, 0, NULL, (ubyte *)"Delete"         },
  45.     { 0, 1, JAM2, 0, 0, NULL, (ubyte *)"Save"           },
  46.     { 0, 1, JAM2, 0, 0, NULL, (ubyte *)"Version"        },
  47.     { 0, 1, JAM2, 0, 0, NULL, (ubyte *)"Remove"         }
  48. };
  49.  
  50. ITEM Item[] = {
  51.     { &Item[1], 0, 0, 100, 10, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP, 0, (APTR)&IText[0], NULL, 'v' },
  52.     { &Item[2], 0,10, 100, 10, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP, 0, (APTR)&IText[1], NULL, 'p' },
  53.     { &Item[3], 0,20, 100, 10, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP, 0, (APTR)&IText[2], NULL, 'd' },
  54.     { &Item[4], 0,30, 100, 10, ITEMTEXT|COMMSEQ|HIGHCOMP, 0, (APTR)&IText[3], NULL, 's' },
  55.     { &Item[5], 0,40, 100, 10, ITEMTEXT|ITEMENABLED|HIGHCOMP, 0, (APTR)&IText[4], NULL},
  56.     { NULL    , 0,50, 100, 10, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP, 0, (APTR)&IText[5], NULL, 'r' }
  57. };
  58.  
  59. MENU Menu[] = {
  60.     { NULL    , 0, 0, 100, 10+50, MENUENABLED, "MailChk", &Item[0] }
  61. };
  62.  
  63. #define NA  0
  64. #define MAILLENGTH 132
  65.  
  66. ubyte Title[128];
  67.  
  68. NW Nw = {
  69.     0, 0, 320, 50, -1, -1,
  70.     NEWSIZE|MENUPICK|CLOSEWINDOW|MOUSEBUTTONS,
  71.     WINSTD|NOCAREREFRESH,
  72.     NULL, NULL, Title, NULL, NULL,
  73.     32, 18, -1, -1, WBENCHSCREEN
  74. };
  75.  
  76. struct MailMsg {
  77.   char msg[MAILLENGTH];
  78.   struct MailMsg *next;
  79. };
  80.  
  81. struct MailMsg *MailP=NULL;
  82.  
  83. WIN *Win = NULL;
  84. RP  *Rp;
  85.  
  86. /* int Enable_Abort; */
  87.  
  88. long IntuitionBase;
  89. long GfxBase;
  90. long ArpBase;
  91.  
  92. main(ac,av)
  93. char *av[];
  94. {
  95.     long chan = NULL;
  96.     long numsecs = 40L;
  97.     char firstrun = 1;
  98.     char *host = NULL;
  99.  
  100.     ArpBase = (long)OpenLibrary(ArpName,39L);
  101.     if (ArpBase == NULL) exit(1);
  102.     {
  103.       register short i;
  104.       for (i = 1; i < ac; ++i) {
  105.         if (strncmp(av[i],"-h",2) == 0) {
  106.             Printf("MAILCHK - Stephane Laroche February 5, 1990\n");
  107.             Printf("DNET    - (C) Matthew Dillon 1988\n");
  108.             Printf("\nUsage:  -t(time) Interval between checks in seconds [DEFAULT = 40 seconds]\n");
  109.             Printf("        -d Display mailbox on start-up [Default]\n");
  110.             Printf("        -n Don't display mailbox on start-up\n");
  111.             Printf("        -h This message...\n");
  112.             exit(0);
  113.         }
  114.         if (strncmp(av[i], "-N", 2) == 0) {
  115.             host = av[i]+2;                
  116.             continue;               
  117.         }
  118.         if (strncmp(av[i],"-d",2) == 0) {
  119.             firstrun = 1;
  120.             continue;
  121.         }
  122.         if (strncmp(av[i],"-n",2) == 0) {
  123.             firstrun = 0;
  124.             continue;
  125.         }
  126.         if (strncmp(av[i],"-t",2) == 0) {
  127.             numsecs = atoi(av[i]+2);
  128.             continue;
  129.         }
  130.       }
  131.     }
  132.  
  133.  
  134.     Enable_Abort = 0;
  135.     IntuitionBase = (long)OpenLibrary("intuition.library", 0);
  136.     GfxBase = (long)OpenLibrary("graphics.library", 0);
  137.     chan = DOpen(host, PORT_MAILCHK, 0, 0);
  138.     if (chan == NULL) {
  139.     Puts("no connect");
  140.     goto fail;
  141.     }
  142.     InitDeemuNW(Deemu+DMNWOFF, &Nw);
  143.     if (initmailserver(chan,numsecs,firstrun))
  144.          checkmail(chan,numsecs);
  145.  
  146. fail:
  147.     if (Win)
  148.     CloseWindow(Win);
  149.     if (chan)
  150.     DClose(chan);
  151.     if (IntuitionBase)
  152.     CloseLibrary(IntuitionBase);
  153.     if (GfxBase)
  154.     CloseLibrary(GfxBase);
  155.     if (ArpBase)
  156.         CloseLibrary(ArpBase);
  157. }
  158.  
  159. initmailserver(chan,numsecs,firstrun)
  160.  
  161. long chan, numsecs;
  162. char firstrun;
  163.  
  164. {
  165.   char init = 4;
  166.  
  167.     if ((DWrite(chan,&init,1) == 1) && (DWrite(chan,&numsecs,4) == 4)
  168.          && (DWrite(chan,&firstrun,1) == 1)) return(1);
  169.     return(0);
  170.  
  171. }
  172. checkmail(chan,numsecs)
  173.  
  174. long chan, numsecs;
  175.  
  176. {
  177.     long imask=0, dmask, mask;
  178.     char notdone = 1, nowindow = 0;
  179.     ULONG prsec = 0, prmic = 0;
  180.     BYTE pos, oldpos = 0;
  181.  
  182.     dmask   = 1 << ((PORT *) chan)->mp_SigBit;
  183.     imask   = 0;
  184.     while (notdone) {
  185.     mask = Wait(imask|dmask|SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_E);
  186.     if (mask & SIGBREAKF_CTRL_C) {
  187.         notdone = 0; break; }
  188.         if (mask & SIGBREAKF_CTRL_E)
  189.             initmailserver(chan,numsecs,2);
  190.     if (mask & imask) {
  191.         IMESS *im;
  192.         while (im = GetMsg(Win->UserPort)) {
  193.         switch(im->Class) {
  194.         case NEWSIZE: 
  195.             dispnewmail(pos,0,1);
  196.             break;
  197.         case MOUSEBUTTONS:
  198.             if (im->Code == SELECTUP) {
  199.               if (DoubleClick(prsec,prmic,im->Seconds,im->Micros)
  200.                           && pos == oldpos) {
  201.             if (pos > 0) {
  202.                 getmailmsg(chan,pos,0);
  203.                         }
  204.               }
  205.               else {
  206.               prsec = im->Seconds;
  207.               prmic = im->Micros;
  208.                 }
  209.               if (pos == 0) dispnewmail(0,oldpos,0);
  210.                       else oldpos = pos;
  211.             }
  212.             else if (im->Code == SELECTDOWN) {
  213.                              register short i=1; 
  214.                              short temp;
  215.                    struct MailMsg *p = MailP;
  216.  
  217.                    temp = (im->MouseY - Win->BorderTop) / Rp->TxHeight + 1;
  218.                    while ( i != temp && p != NULL) { 
  219.                     i++;
  220.                     p = p->next;
  221.                    }
  222.                    if (p == NULL) { oldpos = pos; pos = 0;}
  223.                          else { pos = temp;
  224.                                     dispnewmail(pos,oldpos,0);
  225.                                   }
  226.              }
  227.             break;
  228.         case CLOSEWINDOW:
  229.             nowindow = 1;
  230.             break;
  231.             case MENUPICK:
  232.             switch((uword)((MENUNUM(im->Code)<<8)|ITEMNUM(im->Code))) {
  233.             case 0x0000:    /*    View     */
  234.             getmailmsg(chan,pos,0);
  235.                         break;
  236.             case 0x0001:    /*    Print    */
  237.                         getmailmsg(chan,pos,1);
  238.                         break;
  239.                     case 0x0002:    /*  Delete    */
  240.                         delmailmsg(chan,pos);
  241.                         break;
  242.                     case 0x0003:
  243.                         getmailmsg(chan,pos,2);
  244.                         break;
  245.                     case 0x0004:
  246.                         sprintf(Title, "MailChk V%s%s - S. Laroche 1990", VERSION, MAILCHK_VERSION); 
  247.                         SetWindowTitles(Win,Title,-1);
  248.                         break;
  249.                     case 0x0005:
  250.                         notdone = 0;
  251.                         break;
  252.                     }    
  253.         }
  254.                 ReplyMsg(im);
  255.         }
  256.     }
  257.     if (mask & dmask) {
  258.         char len = 0;
  259.  
  260.         if (DRead(chan, &len, 1) == 1) {
  261.         if (len < sizeof(Title) && DRead(chan, Title, len) == len) {
  262.             Title[len-1] = 0;
  263.             if (strncmp(Title,"No mail",7) == 0) nowindow = 1;
  264.             else { if (strncmp(Title,"New mail",8) == 0) {
  265.                    DisplayBeep(NULL);
  266.                }
  267.                if (Win == NULL) {
  268.                    Win = OpenWindow(&Nw);
  269.                    if (Win != NULL) {
  270.                    Rp = Win->RPort;
  271.                                    SetMenuStrip(Win,Menu);
  272.                    imask   = 1 << Win->UserPort->mp_SigBit;
  273.                    }
  274.                }
  275.                else WindowToFront(Win);
  276.                {
  277.                             short temp=0;
  278.                             char buf[25], buf2[9];
  279.  
  280.                     getnewmail(chan,&temp);
  281.                             if (temp > 1) strcpy(buf2,"messages");
  282.                             else strcpy(buf2,"message");
  283.                 sprintf(buf," (%d %s)",temp,buf2);
  284.                             strcat(Title,buf);
  285.                 dispnewmail(0,0,1);
  286.                            }
  287.                if (Win != NULL) SetWindowTitles(Win, Title, -1);
  288.                            else Puts(Title);
  289.             }
  290.         }
  291.         }
  292.         else notdone = 0;
  293.     }
  294.     if (nowindow && Win) {
  295.             ClearMenuStrip(Win);
  296.         CloseWindow(Win);
  297.         Win = NULL;
  298.         imask = 0;
  299.     }
  300.     nowindow = 0;
  301.     }
  302.  
  303.     freemail(MailP);
  304.     if (Win) {
  305.         ClearMenuStrip(Win);
  306.     CloseWindow(Win);
  307.     Win = NULL;
  308.     }
  309. }
  310.  
  311. /*
  312.  *  Utility routines.    ************************************************
  313.  */
  314.  
  315. getnewmail(chan,count)
  316.  
  317. long chan;
  318. register short *count;
  319.  
  320. {
  321.   char len = 1;
  322.   struct MailMsg *p;
  323.   
  324.   freemail(MailP);
  325.   if (DWrite(chan, &len, 1) == 1 && DRead(chan, &len, 1) == 1) {
  326.       while (len > 0) {   
  327.         if (MailP) { 
  328.             p->next = AllocMem(sizeof(*p),0);
  329.             p = p->next;
  330.         }
  331.         else {
  332.             MailP = AllocMem(sizeof(*p),0);
  333.             p = MailP;
  334.         }
  335.         if (p == NULL) break;
  336.         else p->next = NULL; 
  337.         if (len < MAILLENGTH && (DRead(chan, p->msg, len) == len)) { 
  338.             DRead(chan,&len,1);
  339.             if (Win == NULL) p->msg[len-1] = 0;
  340.             (*count)++;
  341.         }
  342.         else break;
  343.         
  344.       }
  345.   }
  346. }
  347.  
  348. dispnewmail(ONmsgno,OFFmsgno,flag)
  349.  
  350. BYTE ONmsgno, OFFmsgno, flag;
  351.  
  352. {
  353.   char th, tb, tw;
  354.   short y, Wh, Ww, WOx, WOy;
  355.   int len;
  356.   struct MailMsg *p;
  357.   BYTE i=1;
  358.  
  359.   p = MailP;
  360.   if (Win != NULL) {
  361.       th = Rp->TxHeight;
  362.       tb = Rp->TxBaseline;
  363.       tw = Rp->TxWidth;
  364.       y = Win->BorderTop;
  365.       Ww = Win->Width - Win->BorderRight - Win->BorderLeft;
  366.       Wh = Win->Height- Win->BorderTop - Win->BorderBottom;
  367.       WOx = Win->BorderLeft;
  368.       WOy = Win->BorderTop;
  369.       if (flag) {
  370.       SetAPen(Rp, 0);
  371.       RectFill(Rp, WOx, WOy, Ww + WOx, Wh + WOy);
  372.       }
  373.   }
  374.   while (p != NULL && ((Win == NULL) || ((y+tb) < Wh))) {
  375.     short tl;
  376.  
  377.     if (! Win && flag) Puts(p->msg);
  378.     else { if (flag || i == ONmsgno || i == OFFmsgno) {
  379.                if (ONmsgno == i) {
  380.                    SetBPen(Rp,2);
  381.                    SetAPen(Rp,0);
  382.                }
  383.                else {
  384.                    SetAPen(Rp,2);
  385.                    SetBPen(Rp,0);
  386.                } 
  387.                len = strlen(p->msg);
  388.            tl = TextLength(Rp,p->msg,len);
  389.            if (tl > Ww) len = Ww / tw;
  390.            Move(Rp,Win->BorderLeft,y+tb);
  391.            Text(Rp,p->msg,len);
  392.                if (ONmsgno == i) SetAPen(Rp,2);
  393.                else if (OFFmsgno == i || flag) SetAPen(Rp,0);
  394.            RectFill(Rp,Rp->cp_x,y,Ww+WOx,y+th-1);
  395.            }
  396.         y += th;
  397.        i++;
  398.     }
  399.     p = p->next;
  400.   }
  401. }
  402.  
  403. delmailmsg(chan,msgno)
  404.  
  405. long chan;
  406. BYTE msgno;
  407.  
  408. {
  409.   register BYTE i=1;
  410.   struct MailMsg *p = MailP;
  411.   char dummy[32];
  412.   BYTE dl = 3, ok;
  413.   unsigned long stchar = 0,nochars;
  414.  
  415.   for (i=1; (i != msgno && p != NULL); i++) {
  416.     getmailprm(p,&nochars,dummy);
  417.     stchar += nochars;
  418.     p = p->next;
  419.   }
  420.   if (p == NULL) return();
  421.   getmailprm(p,&nochars,dummy);
  422.    
  423.   if ((DWrite(chan,&dl,1) == 1) &&
  424.       (DWrite(chan,&stchar,4) == 4) &&
  425.       (DWrite(chan,&nochars,4) == 4) &&
  426.       (DRead(chan,&ok,1) == 1)) {
  427.       if (ok) {
  428.           freemail(MailP);
  429.           strcpy(Title,"Delete successful, refreshing...");
  430.           SetWindowTitles(Win,Title,-1);
  431.           dispnewmail(0,0,1);
  432.       }
  433.   }
  434. }
  435.  
  436. int getmailmsg(chan,msgno,flag)
  437.  
  438. long chan;
  439. BYTE msgno, flag;
  440.  
  441. {
  442.   struct MailMsg *p = MailP;
  443.   BYTE i, ok=0, hd = 2, l, start = 0;
  444.   unsigned long stchar=0, nochars, len;
  445.   long fh;
  446.   ubyte *buf;
  447.   char pname[128], vname[100], title[132], *tmp;
  448.   int rcode = 1;
  449.   struct NewShell *NS;
  450.   struct FileRequester *FileReq;
  451.  
  452.   if (NS = AllocMem(sizeof(*NS)+4096,MEMF_CLEAR|MEMF_PUBLIC)) {
  453.       NS->nsh_StackSize = 4000;
  454.       NS->nsh_Control = BACKGROUND_SHELL;
  455.   }
  456.   else return(rcode);
  457.   strcpy(vname,"DPIPE:Mail");
  458.   switch (flag) {
  459.     case 0:
  460.       if ((tmp = GetDEnv("PAGER")) == NULL) {
  461.           strcpy(pname,"sys:utilities/more");
  462.       }
  463.       else { strcpy(pname,tmp);
  464.              free(tmp);
  465.       }
  466.       strcat(pname," ");
  467.       strcat(pname,vname);
  468.       strcpy(title,"Viewing ");
  469.       start = 1;
  470.       break;
  471.     case 1:
  472.       strcpy(vname,"PRT:");
  473.       strcpy(title,"Printing ");
  474.       break;
  475.     case 2:
  476.       { /*struct DefaultTracker *TR;*/
  477.  
  478.       strcpy(title,"Saving ...");
  479. /*      FileReq = ArpAllocFreq(); 
  480.       TR = LastTracker; 
  481.       FileReq->fr_Hail = "MAILCHK:  Save to which file?"; 
  482.       FileReq->fr_Dir = "MAIL:";
  483.       FileReq->fr_Window = Win; 
  484.       if (!(FileRequest(FileReq))) {             ->  Doesn't work... why?  
  485.            FreeTrackedItem(TR);  return(rcode);}
  486.       printf("vname= %s\n",vname);
  487.       strcpy(vname,FileReq->fr_Dir);
  488.       TackOn(vname,FileReq->fr_File); 
  489.       FreeTrackedItem(TR); */
  490.       return(rcode);
  491.       } 
  492.       break;
  493.   }
  494.   buf = (ubyte *) (NS + sizeof(*NS));
  495.   l = strlen(title);
  496.   for (i=1; (i != msgno && p != NULL); i++) {
  497.     getmailprm(p,&nochars,title+l);
  498.     stchar += nochars;
  499.     p = p->next;
  500.   }
  501.   if (p == NULL) return(rcode);
  502.   getmailprm(p,&nochars,title+l);
  503.   if (flag == 0) {
  504.       strcat(title," using ");
  505.       strcat(title,pname);
  506.   }
  507.   SetWindowTitles(Win, title, -1);
  508.   if (fh = Open(vname,1006)) {
  509.       if (DWrite(chan, &hd, 1) == 1 && DWrite(chan, &stchar, 4) == 4
  510.       && DWrite(chan,&nochars,4) == 4) {
  511.       while (DRead(chan,&len,4) == 4 && len > 0) {
  512.             if (DRead(chan,buf,len) == len) {
  513.             if (Write(fh,buf,len) != len) {
  514.                     ok = 1;
  515.             DWrite(chan,&ok,1);
  516.             rcode = 0;
  517.             break;
  518.         }
  519.                 if (start) {
  520.                     if (ASyncRun(pname,0L,NS) < 0) {
  521.                         ok = 1;
  522.                         DWrite(chan,&ok,1);
  523.                         rcode = 0;
  524.                         break;
  525.                     }
  526.                     start = 0;
  527.                 }
  528.             DWrite(chan,&ok,1);
  529.         }
  530.           }
  531.       }
  532.       Close(fh);
  533.   }
  534.   else {
  535.     rcode = 0;
  536.   }
  537.   SetWindowTitles(Win,Title,-1);
  538.   FreeMem(NS,sizeof(*NS)+4096);
  539.   return(rcode);
  540. }
  541.  
  542. getmailprm(p,nochars,title)
  543.  
  544. struct MailMsg *p;
  545. unsigned long *nochars;
  546. char *title;
  547.  
  548. {
  549.   char *str;
  550.  
  551.   if (str = index(p->msg,'/')) {
  552.       sscanf(++str,"%ld %s",nochars,title);
  553.   }
  554.   else *nochars = 0;
  555. }
  556.  
  557. freemail(p)
  558.  
  559. struct MailMsg *p;
  560.  
  561. {
  562.   while (p != NULL) {
  563.     FreeMem(p,sizeof(*p));
  564.     p = p->next;
  565.   }
  566.   MailP = NULL;
  567. }
  568.  
  569.  
  570.